package com.kuaifu.autofare.controller.web.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.kuaifu.autofare.bean.model.ParkingEnterprise;
import com.kuaifu.autofare.common.utils.LogUtils;
import com.kuaifu.autofare.common.utils.StringUtils;
import com.kuaifu.autofare.service.IParkingEnterpriseService;

public class SessionFilter implements MyFilter {
    private MyFilter next;

    private Set<String> prefixIignores = new HashSet<String>();

    private IParkingEnterpriseService parkingEnterpriseService;

    public IParkingEnterpriseService getParkingEnterpriseService() {
        return parkingEnterpriseService;
    }

    public void setParkingEnterpriseService(IParkingEnterpriseService parkingEnterpriseService) {
        this.parkingEnterpriseService = parkingEnterpriseService;
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        if (canIgnore(request)) {
            if (next != null) {
                next.doFilter(request, response, chain);
            } else {
                chain.doFilter(request, response);
            }
            return;
        }

        LogUtils.debug("url:" + request.getRequestURI());
        Map<String, Object> extraParams = new HashMap<String, Object>();
        LogUtils.debug(JSONObject.toJSONString(request.getParameterMap()));
        String custId = request.getParameter("custId");
        if (StringUtils.isBank(custId)) {
            try {
                ParkingEnterprise parkingEnterprise = new ParkingEnterprise();
                parkingEnterprise.setAppId(request.getParameter("appId"));
                parkingEnterprise.setOpenId(request.getParameter("openId"));
                ParkingEnterprise enterprise = parkingEnterpriseService.get(parkingEnterprise);

                LogUtils.debug(JSONObject.toJSONString(enterprise));
                if (enterprise != null) {
                    extraParams.put("custId", enterprise.getCustId());
                    RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(request, extraParams);
                    if (next != null) {
                        next.doFilter(requestParameterWrapper, response, chain);
                    } else {
                        chain.doFilter(requestParameterWrapper, response);
                    }
                    return;
                } else {
                    request.getRequestDispatcher("/parkingLotCustomer/page/bindWx").forward(request, response);
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
        prefixIignores = null;
    }

    private boolean canIgnore(HttpServletRequest request) {
        String url = request.getParameter("uri");
        if (StringUtils.isBank(url)) {
            url = request.getRequestURI();
        }
        String contextPath = request.getContextPath();
        for (String ignore : prefixIignores) {
            if (url.startsWith(contextPath + ignore)) {
                return true;
            }
            if (url.startsWith(ignore)) {
                return true;
            }
        }
        return false;
    }

    public Set<String> getPrefixIignores() {
        return prefixIignores;
    }

    public void setPrefixIignores(Set<String> prefixIignores) {
        this.prefixIignores = prefixIignores;
    }

    public MyFilter next() {
        return next;
    }

    public void setNext(MyFilter myFitler) {
        this.next = myFitler;
    }
}
